/********************************************************/
/*	Author:		gong_libin			*/
/*	Date:		2017_11_01			*/
/*	File:		CmEventTest.cpp			*/
/********************************************************/

// http://192.168.21.221?key1=val1&key2=val2&key3=val3
// curl -d "key1=val1&key2=val2&key3=val3" "http://192.168.21.221"

#include "CmEvent.h"

void CmEventRequest(struct evhttp_request* pstReq)
{
	int iSize = 0;
	struct evkeyvalq stKvs;
	const char* pszUri = evhttp_uri_get_query(pstReq->uri_elems);

	/* code */
	switch (pstReq->type) {
		case EVHTTP_REQ_GET:
			CM_PRINT("Code: EVHTTP_REQ_GET\n");
			break;
		case EVHTTP_REQ_POST:
			CM_PRINT("Code: EVHTTP_REQ_POST\n");
			break;
		case EVHTTP_REQ_HEAD:
			CM_PRINT("Code: EVHTTP_REQ_HEAD\n");
			break;
		default:
			CM_PRINT("Code: UNKNOWN\n");
			break;
	}

	/* para */
	evhttp_parse_query_str(pszUri, &stKvs);
	/*CM_PRINT("Key1: %s\n", evhttp_find_header(&stKvs, "key1"));
	CM_PRINT("Key2: %s\n", evhttp_find_header(&stKvs, "key2"));
	CM_PRINT("Key3: %s\n", evhttp_find_header(&stKvs, "key3"));*/
	for (struct evkeyval* pstCur = stKvs.tqh_first; NULL != pstCur; pstCur = pstCur->next.tqe_next) {
		CM_PRINT("Key: %s -> Val: %s\n", pstCur->key, pstCur->value);
	}

	/* head */
	CM_PRINT("Host: %s\n", evhttp_find_header(pstReq->input_headers, "Host"));
	CM_PRINT("Accept: %s\n", evhttp_find_header(pstReq->input_headers, "Accept"));
	CM_PRINT("Connection: %s\n", evhttp_find_header(pstReq->input_headers, "Connection"));
	CM_PRINT("User-Agent: %s\n", evhttp_find_header(pstReq->input_headers, "User-Agent"));
	CM_PRINT("Content-Type: %s\n", evhttp_find_header(pstReq->input_headers, "Content-Type"));
	CM_PRINT("Content-Length: %s\n", evhttp_find_header(pstReq->input_headers, "Content-Length"));
	CM_PRINT("Accept-Encoding: %s\n", evhttp_find_header(pstReq->input_headers, "Accept-Encoding"));
	CM_PRINT("Accept-Language: %s\n", evhttp_find_header(pstReq->input_headers, "Accept-Language"));

	/* body */
	if ((iSize = EVBUFFER_LENGTH(pstReq->input_buffer)) > 0) {
		char szBody[CM_PACKET] = {'\0'};
		memcpy(szBody, EVBUFFER_DATA(pstReq->input_buffer), iSize);
		CM_PRINT("\nContent: %s\n", szBody);
	}

	return;
}

void CmEventResponse(struct evhttp_request* pstReq)
{
	struct evbuffer* pstBuf = evbuffer_new();
	char szHtml[] = "<html><body><h1>网页标题</h1><p>网页正文</p></body></html>";

	evhttp_add_header(pstReq->output_headers, "Connection", "keep-alive");
	evhttp_add_header(pstReq->output_headers, "Cache-Control", "no-cache");
	evhttp_add_header(pstReq->output_headers, "Content-Type", "text/html; charset=utf-8");

	evbuffer_add_printf(pstBuf, "%s", szHtml);
	evhttp_send_reply(pstReq, HTTP_OK, "OK", pstBuf);
	evbuffer_free(pstBuf);

	CM_PRINT("Response: %s\n", szHtml);

	return;
}

void CmEventCore(struct evhttp_request* pstReq, void* pPara)
{
	CM_PRINT((char*)pPara);
	CmEventRequest(pstReq);
	CmEventResponse(pstReq);

	return;
}

int main(int argc, char* argv[])
{
	CCmEvent CEvent;
	const char* c_pszInfo = "\nI'm parameter\n";

	if (CM_SUCCESS == CCmEvent::CmEventInit((char*)"80")) {
		CEvent.CmEventCall(CmEventCore, (void*)c_pszInfo);
		CEvent.CmEventDispatch();
	}

	return CM_SUCCESS;
}
